/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import utils.Customer;

/**
 *
 * @author shaowei
 */
public class RegisterDialog extends javax.swing.JDialog {
    
    private String email ;
    private String password ;
    private String passwordR ;
    
    private LoginDialog loginDlg ;
    

    /**
     * Creates new form RegisterPanel
     */
    public RegisterDialog(LoginDialog dialog) {
        super(dialog,"Register",true);
        loginDlg = dialog ;
        initComponents();
        setVisible(true) ;
       
    }
    
    private void setMessage(final String msg){
        SwingUtilities.invokeLater(new Runnable(){
        
        @Override
        public void run(){
            message.setText(msg) ;
        }
    }) ;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tfEmail = new javax.swing.JTextField();
        btConfirm = new javax.swing.JButton();
        pfPassword = new javax.swing.JPasswordField();
        pfPasswordR = new javax.swing.JPasswordField();
        message = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(400, 300));
        setModal(true);

        jLabel2.setText("E-mail");

        jLabel3.setText("Password");

        jLabel4.setText("Repeat password");

        tfEmail.setColumns(25);
        tfEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfEmailActionPerformed(evt);
            }
        });

        btConfirm.setText("Confirm");
        btConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfirmActionPerformed(evt);
            }
        });

        pfPassword.setColumns(25);

        pfPasswordR.setColumns(25);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(95, 95, 95)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(message)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btConfirm)
                        .addGap(89, 89, 89))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                            .addComponent(pfPassword)
                            .addComponent(pfPasswordR))
                        .addGap(101, 101, 101))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(65, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(pfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(pfPasswordR, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btConfirm)
                    .addComponent(message))
                .addGap(43, 43, 43))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfirmActionPerformed
        // TODO add your handling code here:
         email = tfEmail.getText() ;
         password = new String(pfPassword.getPassword());
         passwordR = new String(pfPasswordR.getPassword());
         
         if(email.equals("") || password.equals("") || passwordR.equals("")){
             setMessage("please fill all the blanks! ") ;
         }
         else{
                if(!password.equals(passwordR)) {
                setMessage("Two passwords do not match !") ;
         }
                else {
                    utils.EmailValidator emailValidator = new utils.EmailValidator();
                    if(!emailValidator.validate(email)) {
                    setMessage("Invalid Email ID");
                    }
                    
                    else{
                    try {
                        boolean userRegistered = Customer.register(email, password) ;
                        
                        if(userRegistered){
                            
                            loginDlg.setMessage("Register successfully, please log in! ") ;
                            RegisterDialog.this.dispose() ;
                            
                        }
                            else
                            setMessage("Register failed! ") ;
                    } catch (RemoteException ex) {
                        Logger.getLogger(RegisterDialog.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                }
                }
        
    }//GEN-LAST:event_btConfirmActionPerformed

    private void tfEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfEmailActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_tfEmailActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btConfirm;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel message;
    private javax.swing.JPasswordField pfPassword;
    private javax.swing.JPasswordField pfPasswordR;
    private javax.swing.JTextField tfEmail;
    // End of variables declaration//GEN-END:variables
}
